﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Controllers
{
    public class UserLoginController : BaseController
    {
        public UserLoginController()
        {
            this.IsCheckUserLogin = false;
        }
        public IUserInfoService UserInfoService { get; set; }
        public IRoleInfoService RoleInfoService { get; set; }
        public IDepartmentService DepartmentService { get; set; }
        public IUserDepartmentService UserDepartmentService { get; set; }
        // GET: UserLogin
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 处理登录
        /// </summary>
        /// <returns></returns>
        public string ProcessLogin()
        {
            JsonMessage msg = new JsonMessage();
            string name = Request["LoginCode"];
            string pwd = Request["LoginPwd"];
            short delNormal = (short)DelFlagEnum.Normal;

            var userInfo = UserInfoService.GetEntities(u => u.UName == name && u.Pwd == pwd && u.DelFlag == delNormal)
                .FirstOrDefault();
            if (userInfo == null)
            {
                msg = JsonHandler.CreateMessage(0, "用户名密码错误!");
                return JsonHandler.ToJson(msg);
            }
            //获取用户权限
            List<RoleInfo> roleList = new List<RoleInfo>();
            foreach (var role in userInfo.RoleInfo)
            {
                roleList.Add(role);
            }
            var deptIdList = UserDepartmentService.GetEntities(u => u.UserId == userInfo.Id).ToList();
            List<Department> deptList = new List<Department>();
            foreach (var item in deptIdList)
            {
                Department dept = DepartmentService.GetModel(item.DeptId ?? 0);
                deptList.Add(dept);
            }
            UserRight userRight = new UserRight
            {
                User = userInfo,
                RoleList = roleList,
                DeptList = deptList
            };

            //Session["loginUser"] = userInfo;
            //用memcahce+cookie代替Session
            //立即分配一个标志，guid.把标志作为mm存储数据的key，把用户对象放到mm.把guid写到客户端cookie里面去
            string userLoginId = Guid.NewGuid().ToString();
            //把用户数据写到mm
            Common.CacheHelper.AddCache(userLoginId, userInfo, DateTime.Now.AddHours(4));
            Common.CacheHelper.AddCache(userInfo.Id.ToString(), userRight, DateTime.Now.AddHours(4));
            Response.Cookies["userLoginId"].Value = userLoginId;

            msg = JsonHandler.CreateMessage(1, "登录成功!", JsonHandler.ToJson(userInfo));

            return JsonHandler.ToJson(msg);

        }
        /// <summary>
        /// 用户退出
        /// </summary>
        /// <returns></returns>
        public string ProcessLogout()
        {
            string res = string.Empty;
            string userGuid = Request.Cookies["userLoginId"].Value;
            //清楚缓存
            Common.CacheHelper.SetCache(userGuid, null, DateTime.Now.AddHours(-1));
            //清楚cookie
            HttpCookie aCookie = new HttpCookie("userLoginId");
            aCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(aCookie);
            res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "用户退出成功!"));
            return res;
        }

    }
}